/********************************************************
 * Filename: TclEditor.java
 * Author: LMU
 * Program: TUnit
 * Description: Creates the tcl editor for eclipse.  
 * Revision History:
 * 20060418: Initial Revision.
 * 20060927: Added getTclSourceViewer for tesing 
 * purposes.  
 * *****************************************************/

package edu.lmu.cs.tUnit.editor;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.editors.text.TextEditor;

import edu.lmu.cs.tUnit.plugin.TUnitPlugin;
import edu.lmu.cs.tUnit.preferences.TclPreferencesConstants;

public class TclEditor extends TextEditor implements IPropertyChangeListener {
	
	private SoftTabListener softTabListener;
	
	public TclEditor() {
		super();
		TclConfiguration config = new TclConfiguration();
		TclStringScanner scanner = config.getTclStringScanner();
		TclDocumentProvider provide = new TclDocumentProvider();
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		provide.setTclStringScanner(scanner);
		setSourceViewerConfiguration(config);
		setDocumentProvider(provide);
		softTabListener = new SoftTabListener();
		softTabListener.setUseSoftTab(store.getBoolean(TclPreferencesConstants.P_USE_SPACES_FOR_TABS));
		softTabListener.setSoftTabWidth(store.getInt(TclPreferencesConstants.P_NUMBERSPACES));
		TUnitPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
	}

	/**
	 * Check if the preferences have changed.  
	 */
	public void propertyChange(PropertyChangeEvent event) {
		ISourceViewer sourceViewer = getSourceViewer();
		if (sourceViewer == null) {
			return;
		}
				
		String prop = event.getProperty();	
		if ( (prop.equals(TclPreferencesConstants.P_COMMENTCOLOR)) || (prop.equals(TclPreferencesConstants.P_KEYWORDCOLOR1)) ||
				(prop.equals(TclPreferencesConstants.P_KEYWORDCOLOR2)) || (prop.equals(TclPreferencesConstants.P_STRINGCOLOR)) ||
				(prop.equals(TclPreferencesConstants.P_DEFAULTCOLOR)) || (prop.equals(TclPreferencesConstants.P_BACKGROUNDCOLOR))) {
			((TclConfiguration)getSourceViewerConfiguration()).updateScanner();
			setBackgroundColor();
		}
		
		softTabListener.preferenceChanged(event);
		((TclConfiguration)getSourceViewerConfiguration()).updateContentAssist();
	}
	
	/**
	 * Change background color of the editor
	 *
	 */
	private void setBackgroundColor() {
		ISourceViewer sourceViewer = getSourceViewer();
		if(sourceViewer == null || sourceViewer.getTextWidget() == null) 
			return;
			
		Control widget = sourceViewer.getTextWidget();
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		Color background = new Color(Display.getCurrent(), PreferenceConverter.getColor(store, TclPreferencesConstants.P_BACKGROUNDCOLOR));
		widget.setBackground(background);
		sourceViewer.invalidateTextPresentation();
		widget.redraw();
	}
	
	/**
	 * Override to make use of soft tabs
	 */
	protected ISourceViewer createSourceViewer(Composite parent,IVerticalRuler ruler, int styles) {
		ISourceViewer viewer= new ProjectionViewer(parent, ruler, fOverviewRuler, true, styles); 
		getSourceViewerDecorationSupport(viewer);
		viewer.getTextWidget().addVerifyListener(softTabListener);
		return viewer; 
	}
	
	/**
	 * Override to set the initial color of the backgound
	 */
    public void createPartControl(Composite parent) {
    	super.createPartControl(parent);
    	ISourceViewer sourceViewer = getSourceViewer();
		Control widget = sourceViewer.getTextWidget();
		IPreferenceStore store = TUnitPlugin.getDefault().getPreferenceStore();
		Color background = new Color(Display.getCurrent(), PreferenceConverter.getColor(store, TclPreferencesConstants.P_BACKGROUNDCOLOR));
		widget.setBackground(background);
    }
    
    /**
     * Return the SourceViewer for testing purposes
     */
    public ISourceViewer getTclSourceViewer() {
    	return getSourceViewer();
    }
}
